<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 9:05
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Member2 extends Base {

    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($phone) && $phone != ''){
                $map['a.user_phone|a.user_id'] = ['like', '%' . $phone . '%'];
            }
            //注册时间
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.user_time'] = ['between time',[$start,$end]];
            }
            $map['user_status'] =array('neq',2);
            $map['user_role'] = 3;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->alias('a')
                ->where($map)->count();// 获取总条数
            $count2 = Db::name('u_user')->alias('a')
                ->where($map)->column('user_id');// 获取总条数
            $lists = Db::name('u_user')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('user_time desc')
                ->select();
//            foreach ($lists as $key=>&$vo){
//                if($vo['user_status']==1){
//                    $vo['user_status_name'] = '正常';
//                }else if($vo['user_status']==3){
//                    $vo['user_status_name'] = '冻结';
//                }
//            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *解冻
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *冻结
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',3);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }


    public function outExcel(){

        $user_ids = input('ids');
        if($user_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的用户']);
        }
        $lists = Db::name('u_user')
            ->field('user_id, user_phone, user_username, user_card, user_company_name,user_company_address')
            ->where('user_id', 'IN', $user_ids)
            ->order('user_time desc')
            ->select();
        foreach($lists as $k => $v){
            $lists[$k]['user_phone'] = "\t".$v['user_phone']."\t";
            $lists[$k]['user_card'] = "\t".$v['user_card']."\t";
        }
        $cellname = [
            ['user_id','ID',20,'LEFT'],
            ['user_phone','手机号',20,'LEFT'],
            ['user_username','昵称',20,'LEFT'],
            ['user_card','身份证号码',20,'LEFT'],
            ['user_company_name','公司名称',20,'LEFT'],
            ['user_company_address','公司地址',20,'LEFT']
        ];
        $res = exportExcel('加盟商用户列表', 'Looks', $cellname, $lists);
        return json($res);
    }

    public function add(){
        if(Request::instance()->isPost()){
            $phone = input('user_phone');
            $user_password = input('param.user_password');
            $user_username = input('param.user_username');
            $user_image = input('param.user_image');
            $user_card= input('param.user_card');
            $user_z_card_img= input('param.user_z_card_img');
            $user_f_card_img= input('param.user_f_card_img');
            $user_yyzz= input('param.user_yyzz');
            $user_company_name= input('param.user_company_name');
            $user_company_address= input('param.match_addr');
            $user_real_name= input('param.user_real_name');
            $match_lon= input('param.match_lon');
            $match_lat= input('param.match_lat');
            if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
                return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
            }
            $info = Db::table('up_u_user')->where('user_phone',$phone)->find();
            if($info['user_id']!=''){
                return json_encode(['status'=>1,'msg'=>'此号码已经注册,请更换手机号码']);
            }
            $data['user_username'] = $user_username;
            $data['user_card'] = $user_card;
            $data['user_z_card_img'] = $user_z_card_img;
            $data['user_f_card_img'] = $user_f_card_img;
            $data['user_company_name'] = $user_company_name;
            $data['user_jms_addr'] = $user_company_address;
            $data['user_image'] = $user_image;
            $data['user_phone'] = $phone;
            $data['user_password'] = md5($user_password);
            $data['user_pass'] = $user_password;
            $data['user_match_lon'] = $match_lon;
            $data['user_match_lat'] = $match_lat;
            $data['user_real_name'] = $user_real_name;
            $data['user_status'] = 1 ;
            $data['user_role'] = 3;
            $data['user_time'] = date('Y-m-d H:i:s');
            $data['user_yyzz'] = $user_yyzz;
            if($user_yyzz!=''){
                $result1 =$this->yyzz($user_yyzz) ;

                $data['user_company_name'] = $result1['result']['res'][8]['value'];
                $data['user_company_detail'] = $result1['result']['res'][7]['value'];
                $data['user_jms_address'] = $result1['result']['res'][7]['value'];
            }
            $res = Db::table('up_u_user')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $phone = input('user_phone');
            $user_password = input('param.user_password');
            $user_username = input('param.user_username');
            $user_image = input('param.user_image');
            $user_card= input('param.user_card');
            $user_z_card_img= input('param.user_z_card_img');
            $user_f_card_img= input('param.user_f_card_img');
            $user_company_name= input('param.user_company_name');
            $user_company_address= input('param.match_addr');
            $user_real_name= input('param.user_real_name');
            $match_lon= input('param.match_lon');
            $match_lat= input('param.match_lat');
            $user_yyzz= input('param.user_yyzz');

            if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
                return json_encode(['status'=>1,'msg'=>'请填写正确的手机号码']);
            }
            $where['user_phone'] = $phone;
            $where['user_id'] = array('neq',$id);
            $info = Db::table('up_u_user')->where($where)->find();
            if($info['user_id']!=''){
                return json_encode(['status'=>1,'msg'=>'此号码已经注册,请更换手机号码']);
            }
            if(!empty($user_image)){
                $data['user_image'] = $user_image;
            }
            if(!empty($user_yyzz)){
                $data['user_yyzz'] = $user_yyzz;
            }
            if(!empty($user_z_card_img)){
                $data['user_z_card_img'] = $user_z_card_img;
            }
            if(!empty($user_f_card_img)){
                $data['user_f_card_img'] = $user_f_card_img;
            }
            if(!empty($user_yyzz)){
                $result1 =$this->yyzz($user_yyzz) ;
                $data['user_company_name'] = $result1['result']['res'][8]['value'];
                $data['user_company_detail'] = $result1['result']['res'][7]['value'];
                $data['user_jms_address'] = $result1['result']['res'][7]['value'];
            }
            $data['user_match_lon'] = $match_lon;
            $data['user_match_lat'] = $match_lat;
            $data['user_real_name'] = $user_real_name;
            $data['user_card'] = $user_card;
            $data['user_company_name'] = $user_company_name;
            $data['user_jms_address'] = $user_company_address;
            $data['user_username'] = $user_username;
            $data['user_phone'] = $phone;
            $data['user_password'] = md5($user_password);
            $data['user_pass'] = $user_password;
            $res = Db::table('up_u_user')->where('user_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.user_id');
        $info = Db::table('up_u_user')->where('user_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function detail(){
        $id = input('param.user_id');
        $info = Db::table('up_u_user')->where('user_id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    //营业执照
    public function yyzz($user_yyzz){
        header('Content-type:text/html;charset=utf-8');
        $appkey1 = "f3cc570bd987000b15338314e118314f";//营业执照
        $user_yyzz1 = master_imgUrl_base64($user_yyzz);//营业执照转base64
        $url1 = "http://apis.juhe.cn/businessLicenseOcr/index";
        $params1 = array(
            "image" => $user_yyzz1,//营业执照
            "key" => $appkey1,//应用APPKEY(应用详细页查询)
        );
        $paramstring1 = http_build_query($params1);
        $content1 = juhecurl($url1,$paramstring1,1);
        $result = json_decode($content1,true);
        return $result;
    }
}